<?php
// @formatter:off
/**
 * @file c.search.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.search.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/themes/mdefault/controlers/contents/c.search.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_SPECIAL_SEARCH)) {
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcGroup;
	global $wcSearch;
	global $wcDefaults;
	global $wctDefaults;

	$wcThemeAssigns["PATH"] = "Search";
	$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.search.html";

	$wcThemeAssigns["search-items"] = array();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minSrchInPage = $wctDefaults["max-per-page"]["search"] * ($page - 1);
	$maxSrchInPage = $minSrchInPage + $wctDefaults["max-per-page"]["search"];

	$count = 0;
	if(isset($_REQUEST["pattern"])) {
		$onlyTypes = array(
			"CL",
			"TA",
			"CX"
		);
		$pattern = WCSearchFactory::StringToPatterns(turldecode($_REQUEST["pattern"]));
		$wcThemeAssigns["search-pattern"] = implode(" ", $pattern);
		$exceptions = WCSearchFactory::StringToPatterns(turldecode(@$_REQUEST["exceptions"]));
		$wcThemeAssigns["search-exceptions"] = implode(" ", $exceptions);

		if(isset($_REQUEST["type"])) {
			$wcThemeAssigns["only-type"] = $_REQUEST["type"];
			$onlyTypes = array($_REQUEST["type"]);
		} else {
			$wcThemeAssigns["only-type"] = "";
		}

		$result = $wcSearch->get($pattern, $wcGroup, true, $onlyTypes, $exceptions);
		$result_count = count($result);
		$wcThemeAssigns["result-count"] = $result_count;

		$findings = array();
		$iconId = 1;
		$zoom = array();
		if($result_count) {
			$ids = 1;
			foreach($result as $item) {
				if($count >= $minSrchInPage && $count < $maxSrchInPage) {
					$aux = array(
						"name" => "{$item->type()}: {$item->name()}",
						"title" => "{$item->type()}: {$item->name()}",
						"iconid" => $iconId,
						"url" => $item->link()
					);
					if($item->hasPreviews()) {
						$previews = $item->previews();
						$aux["icon"] = isset($previews[0]) ? $previews[0] : $wcDefaults['preview'];

						$zoom[] = array(
							"id" => $iconId,
							"path" => $aux["icon"]
						);
					}

					$findings[] = $aux;

					$iconId++;
				}
				$count++;
			}
		}

		$pages = ceil($result_count / $wctDefaults["max-per-page"]["search"]);
		wcSetPager(requestParams(true, $wcAction, array('page')), $pages, $page);

		$wcThemeAssigns["MenuLists"] = array( array(
				"prefix" => "Results",
				"list" => $findings
			));
		$wcThemeAssigns["MenuListsZoom"] = $zoom;
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to search items";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>